package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.reject

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.IApiProcessCameroonCallback
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.createApiActionCameroon
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.doPostRequestCameroon
import org.json.JSONArray
import org.json.JSONObject
import java.math.BigDecimal

class RejectCameroonViewModel : ViewModel() {

    private val _loadingResult = MutableLiveData<Boolean>()
    val loadingResult: LiveData<Boolean> = _loadingResult

    private val _pageDetailResult = MutableLiveData<RejectCameroonResult>()
    val pageDetailResult: LiveData<RejectCameroonResult> = _pageDetailResult

    private val _walletResult = MutableLiveData<RejectCameroonWalletResult>()
    val walletResult: LiveData<RejectCameroonWalletResult> = _walletResult

    private val _commendProductsResult = MutableLiveData<RejectCameroonCommendProductsResult>()
    val commendProductsResult: LiveData<RejectCameroonCommendProductsResult> = _commendProductsResult

    /** 获取页面详情接口 */
    fun pageDetail() {
        _loadingResult.value = true
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "WvSc3ghpalrgbjmUXBKvvg"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false
                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                // 去掉无效0
                val amount = obj.optString("amount")
                val amountString = BigDecimal(amount).stripTrailingZeros().toPlainString()
                obj.put("amount", amountString)

                val currency = obj.optString("currency")
                val amountFormat = "$amountString $currency"
                obj.put("amountFormat", amountFormat)

                val days = obj.optString("days")
                obj.put("daysFormat", "$days jours")
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false
                _pageDetailResult.value = RejectCameroonResult(error = errMsg)
            }
        })

    }

    /** 获取推荐产品接口 */
    fun commendProducts() {
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "na7K1WhPIBpMqSF635Spgg"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val obj = jsonObject.optJSONArray("obj") ?: JSONArray()
                val list = mutableListOf<JSONObject>()
                for (index in 0 until obj.length()) {
                    val item = obj[index] as JSONObject
                    list.add(item)
                }
                _commendProductsResult.value =
                    RejectCameroonCommendProductsResult(success = list)
            }

            override fun onApiError(code: String, errMsg: String) {
                _commendProductsResult.value =
                    RejectCameroonCommendProductsResult(error = errMsg)
            }
        })
    }
}